package com.why.controller;

import com.why.entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author hy woo
 * @date 2022/6/8 12:04
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @RequestMapping("/upload")
    public Result upload(MultipartFile myFile) throws IOException {
        if (null!=myFile && !myFile.isEmpty()){
            String s = myFile.getOriginalFilename();
            String s1 = s.substring(s.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString() + s1;
            myFile.transferTo(new File("d:/pic/picimg"+fileName));
            return new Result(200,"","http://localhost:8080/picimg/"+fileName);
        }
        return new Result(300,"文件不存在","");
    }
}
